package info.liujiachen.security.test.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JwtAuthenticationFailureHandler implements AuthenticationFailureHandler {

    // 日志
    private Logger logger = LoggerFactory.getLogger(getClass());

    public JwtAuthenticationFailureHandler() {
    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        logger.error("Error Type: " + e.getClass().getSimpleName());
        logger.error("Error Message: ", e.getMessage());
        response.sendError(HttpStatus.FORBIDDEN.value(), e.getMessage());
    }

}
